{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# [1] SciPy 优化器\n",
    "# SciPy 的 optimize 模块提供了常用的最优化算法函数实现，我们可以直接调用这些函数完成我们的优化问题，比如查找函数的最小值或方程的根等。\n",
    "# \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    fjac: array([[-1.]])\n",
      "     fun: array([0.])\n",
      " message: 'The solution converged.'\n",
      "    nfev: 9\n",
      "     qtf: array([-2.66786593e-13])\n",
      "       r: array([-1.67361202])\n",
      "  status: 1\n",
      " success: True\n",
      "       x: array([-0.73908513])\n",
      "[-0.73908513]\n"
     ]
    }
   ],
   "source": [
    "# [2] SciPy 优化器 - optimize.root()\n",
    "# 查找 x + cos(x) 方程的根:\n",
    "# optimze.root 函数需要两个参数：\n",
    "# fun - 表示方程的函数。\n",
    "# x0 - 根的初始猜测。\n",
    "# \n",
    "from scipy.optimize import root\n",
    "from math import cos\n",
    "\n",
    "# 方程 y = x + cos(x)\n",
    "# \n",
    "def eqn(x):\n",
    "  return x + cos(x)\n",
    "\n",
    "\n",
    "# 求方程的根\n",
    "myroot = root(eqn, 0)\n",
    "print(myroot)\n",
    "print(myroot.x)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "      fun: 1.75\n",
      " hess_inv: array([[0.50000001]])\n",
      "      jac: array([0.])\n",
      "  message: 'Optimization terminated successfully.'\n",
      "     nfev: 8\n",
      "      nit: 2\n",
      "     njev: 4\n",
      "   status: 0\n",
      "  success: True\n",
      "        x: array([-0.50000001])\n",
      "[-0.50000001]\n"
     ]
    }
   ],
   "source": [
    "# [3] SciPy 优化器 - optimize.minimize()\n",
    "# 最小化函数\n",
    "# 函数表示一条曲线，曲线有高点和低点。\n",
    "# 高点称为最大值。\n",
    "# 低点称为最小值。\n",
    "# 整条曲线中的最高点称为全局最大值，其余部分称为局部最大值。\n",
    "# 整条曲线的最低点称为全局最小值，其余的称为局部最小值。\n",
    "# 可以使用 scipy.optimize.minimize() 函数来最小化函数。\n",
    "# minimize() 函接受以下几个参数：\n",
    "# fun - 要优化的函数\n",
    "# x0 - 初始猜测值\n",
    "# method - 要使用的方法名称，值可以是：'CG'，'BFGS'，'Newton-CG'，'L-BFGS-B'，'TNC'，'COBYLA'，，'SLSQP'。\n",
    "# callback - 每次优化迭代后调用的函数。\n",
    "# options - 定义其他参数的字典：\n",
    "# {\n",
    "#     \"disp\": boolean - print detailed description\n",
    "#     \"gtol\": number - the tolerance of the error\n",
    "# }\n",
    "# \n",
    "from scipy.optimize import minimize\n",
    "\n",
    "# 实例\n",
    "# x^2 + x + 2 使用 BFGS 的最小化函数:\n",
    "#\n",
    "def eqn(x):\n",
    "  return x**2 + x + 2\n",
    "\n",
    "\n",
    "mymin = minimize(eqn, 0, method='BFGS')\n",
    "print(mymin)\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.9.9 64-bit",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.9"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "cf18841ace8313d0bc088ca146c17a6c0040e82121d5cb75c0ea07172309253d"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
